package com.my.crypto;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.nio.charset.StandardCharsets;

/**
 * @author: marshal
 * @date: 2021-11-09 11:45 AM
 */
public class RSATest {
    /**
     * 本地rsa已经生成的秘钥
     */
    private static final String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALWeCPMmHw0+Glo7BeV5nxTgPhoUlaYWgocPA0/ofgTQPiRpkpzVPvrQy21ppPryuf/BHZZ8yxYbF7fqCF1DSGdiCkKwVQkQAyUJvJhEKcQh8PhGjvbmNpCqznnYKyEAtTEHbW4FD/8nWdILvpPRD900tlMfVFRCpgVLs6vsq9kNAgMBAAECgYASJ3zviTT7ZuTNdi7ttE2RkaTDiKE42/EqPPbwyuWl54b+rsroAGzuN2bORgwmxZKQHSau/0Rxrj2dtbgmUm5vM9uf8CAQzK2zzTWZF5e7CWFGO+2VgFosCpl30EmlNG/LfaRLRtQZ1U7kJSnHTngDa+bbD3d9KG8E5Q3UdQT9wQJBAOqhXb1DW7ZPAT3PQ5lgCgw5ROQGZweqPeuzI/MtEgmsxiRRmSLCPF1wjUaKHut5Z4Qt/7u135H47jZJDbL+QHECQQDGKJ52aPr4JTZ6tRXwgpSQzRdqqzUX92uv7LMG5Ol8Z/dRAST9NrfUtR7w0aYqHjiTLJJyzIEetn7BNtHabXBdAkAMDeRJ//brQ93VdhuL+rIpdJvEd0u9yhJKVLbJHIyrsJOxc/vBHGeLiWfIsvnpsSAPW2mjFDH7RuW+Jjys8IERAkBGjyAdubPjuBx2BRUS1uxfwyeba+sz5X4K3pXA1rHtAngl9+pb36gFrZie14f7Z1dt0Gk1R2ySqxjnY8XFqUJhAkEAla9rU3IK1Lp6mb7uzdkAsf8rU2dgownBRxkhyLtoFjE28kklvBD5yRM1r9HdeRGMPfQSwP2cvdnm33rOFxm1CA==";

    /**
     * 服务端rsa生成的公钥
     */
    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1ngjzJh8NPhpaOwXleZ8U4D4aFJWmFoKHDwNP6H4E0D4kaZKc1T760MttaaT68rn/wR2WfMsWGxe36ghdQ0hnYgpCsFUJEAMlCbyYRCnEIfD4Ro725jaQqs552CshALUxB21uBQ//J1nSC76T0Q/dNLZTH1RUQqYFS7Or7KvZDQIDAQAB";

    public static void main(String[] args) throws Exception {
        test4();
    }


    private static void test4() {
        String s = "XUFhmBGNYlzWZkJULvCE1edEXDu3bFt3p3ZlpOj7dJwKq7ytDfUjQU0Xh1h4DJoilIx67bBiz72qY9UYU97J+7qcILSkRGIpj3XNCkynNGBIY1eZQC7D8WQt+XtNp3SPvie/0OvRaGilPj1uXuZ2Tj7sSQ7mA0LGssCYhYP5lY0=";
        RSA rsa = new RSA("RSA/None/NoPadding", PRIVATE_KEY, PUBLIC_KEY);
        System.out.println(rsa.decryptStr(s, KeyType.PublicKey));
    }

    private static void test3(){
        RSA rsa = new RSA("RSA", PRIVATE_KEY, PUBLIC_KEY);
        String s = "OAfp8nStU+3L+oNtZUc7Ajv8Q7OImf4fDPPWM6nrongY30OjJoO6fHhm2ipkmZM86lUi9f5OtMwvP3IGJq0jGBVhNTY9/ZufPj2GWTNtN64bol+v/RikcY6R5Hifr7yjNt9AUR1VvFrR9IfDtdtvba6asvWk7km+0SWsslu6/f8=";
        System.out.println(rsa.decryptStr(s, KeyType.PublicKey));
    }

    private static void test2() {
        String password = "SuperAdmin";
        RSA rsa = new RSA("RSA", PRIVATE_KEY, PUBLIC_KEY);
        String encryptBase64 = rsa.encryptBase64(password, KeyType.PrivateKey);
        System.out.println(encryptBase64);
        String s = rsa.decryptStr(encryptBase64, KeyType.PublicKey, CharsetUtil.CHARSET_UTF_8);
        System.out.println(s);
    }

    private static void test1() {
        String password = "SuperAdmin";
        RSA rsa = new RSA(PRIVATE_KEY, PUBLIC_KEY);

        byte[] encrypt = rsa.encrypt(StrUtil.bytes(password, StandardCharsets.UTF_8), KeyType.PrivateKey);
        String encryptStr = StrUtil.str(encrypt, StandardCharsets.UTF_8);
        System.out.println(encryptStr);

        byte[] decrypt = rsa.decrypt(StrUtil.bytes(encryptStr, StandardCharsets.UTF_8), KeyType.PublicKey);
        String decryptStr = StrUtil.str(decrypt, StandardCharsets.UTF_8);
        System.out.println(decryptStr);
    }

    private static void test() {
        String password = "SuperAdmin";
        RSA rsa = new RSA(PRIVATE_KEY, PUBLIC_KEY);
        //公钥加密，私钥解密
        byte[] encrypt = rsa.encrypt(StrUtil.bytes(password, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
        byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
        String str = StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8);
        System.out.println(str);
        System.out.println(password.equals(str));


    //私钥加密，公钥解密
        byte[] encrypt2 = rsa.encrypt(StrUtil.bytes(password, CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
        byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
        System.out.println(password.equals(StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8)));

    }

}
